//
//  WHStatusViewModel.swift
//  WHSinaWeiBo
//
//  Created by 王寒 on 16/4/12.
//  Copyright © 2016年 wanghan. All rights reserved.
//

import UIKit

class WHStatusViewModel: NSObject {
    //模型
    var status: WHStatus?
    //mb
    var mbImage: UIImage?
    //认证类型图片
    var verifiedImage: UIImage?
    
    //转发的文字
    var respostTitle: String?
    var commentTitle: String?
    var ohYeahTitle: String?
    
    var headImageURL: NSURL?
    
    init(status: WHStatus) {
        super.init()
        //记录参数
        self.status = status
        //headImageUrl
        self.headImageURL = NSURL(string: status.user?.profile_image_url ?? "")

        //用户等级
        dealmbRank()
        //用户认证类型
        dealVerifiedType()
        
        respostTitle = dealToolBarText(status.reposts_count ?? 0, defaultTitle: "转发")
        commentTitle = dealToolBarText(status.comments_count ?? 0, defaultTitle: "评论")
        ohYeahTitle = dealToolBarText(status.attitudes_count ?? 0, defaultTitle: "赞")
        
    }
    
}

//处理微博数据
extension WHStatusViewModel{
    //根据数量计算显示文字 如果大于0 就显示数量 如果小于0就显示原有文字
    private func dealToolBarText(count: Int,defaultTitle: String) -> String {
        
        if count < 0{
            return defaultTitle
        }
        
        if count > 10000{
            return "\(Double(count)/10000.0)万"
        }
        
        return "\(count)"
        
    }
    
    
    //用户等级
    private func dealmbRank(){
        let mbType = status?.user?.mbrank ?? 0
        if mbType > 0 && mbType < 7 {
            mbImage = UIImage(named: "common_icon_membership_level\(mbType)")
        }
    }
    
    //用户认证类型
    private func dealVerifiedType(){
        let verifiedType = status?.user?.verifiedtype ?? 0
        switch verifiedType {
        case 1:
            verifiedImage = UIImage(named: "avatar_vip")
        case 2,3,5:
            verifiedImage = UIImage(named: "avatar_enterprise_vip")
        case 220:
            verifiedImage = UIImage(named: "avatar_grassroot")
        default:
            verifiedImage = nil
        }
    }
    
}
